package com.kakaxi.mybilibili.module.home.category.view;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.CategoryHomeRecyAdapter;
import com.kakaxi.mybilibili.base.BaseFragment;
import com.kakaxi.mybilibili.base.MyApplication;
import com.kakaxi.mybilibili.bean.CategoryHomeItemBean;

import java.io.InputStream;
import java.util.List;

import butterknife.BindDimen;
import butterknife.ButterKnife;

/**
 * 分区碎片
 * Created by kakaxi on 16/10/11.
 */

public class CategoryHomeFragment extends BaseFragment {

    @BindDimen(R.dimen.default_distance)
    int padding;

    /** 分区回收列表视图 **/
    private RecyclerView mCategoryRecView;

    /** 分区信息项列表 **/
    private List<CategoryHomeItemBean> mCategoryHomeBeenList;

    private SwipeRefreshLayout mSwipeRefreshLayout;

    public static CategoryHomeFragment newInstance() {
        return new CategoryHomeFragment();
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_common_recycler;
    }

    @Override
    protected void setupViews(ViewGroup viewGroup) {
        ButterKnife.bind(this, viewGroup);
        mCategoryRecView = (RecyclerView) viewGroup.findViewById(R.id.recv_common_fragment);
        mCategoryRecView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        mCategoryRecView.setPadding(0, padding, 0, 0);

        mSwipeRefreshLayout = (SwipeRefreshLayout) viewGroup.findViewById(R.id.srl_common_fragment);
        mSwipeRefreshLayout.setEnabled(false);

        mCategoryRecView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int pos) {
                CategoryHomeItemBean itemBean = (CategoryHomeItemBean) baseQuickAdapter.getItem
                        (pos);
                Bundle bundle = new Bundle();
                bundle.putString(CategoryAreaActivity.EXTRA_TYPENAME, itemBean.getTypename());
                bundle.putInt(CategoryAreaActivity.EXTRA_TID, itemBean.getTid());
                // 通过app管理器传递参数
                ((MyApplication) getActivity().getApplication()).putShareData
                        (CategoryAreaActivity.EXTRA_SHARE_CHILD_LIST, itemBean.getChild());

                goActivity(CategoryAreaActivity.class, bundle);
            }
        });
    }

    @Override
    protected void initData() {
        try {
            InputStream is = getResources().getAssets().open("category_home_array.xml");
            PullCategoryHomeParser parser = new PullCategoryHomeParser();
            mCategoryHomeBeenList = parser.parse(getContext(), is);
        } catch (Exception e) {
            e.printStackTrace();
        }

        mCategoryRecView.setAdapter(new CategoryHomeRecyAdapter(mCategoryHomeBeenList));
    }

    private void initCategoryData() {
//        mCategoryHomeBeenList = new ArrayList<>(14);
//        mCategoryHomeBeenList.add(new CategoryHomeItemBean("直 播", getDrawable(R.drawable
//                .ic_category_live), null));
//        mCategoryHomeBeenList.add(new CategoryHomeItemBean("番 剧", getDrawable(R.drawable
//                .ic_category_t13), null));
//        mCategoryHomeBeenList.add(new CategoryHomeItemBean("动 画", getDrawable(R.drawable
//                .ic_category_t1), null));
//        mCategoryHomeBeenList.add(new CategoryHomeItemBean("音 乐", getDrawable(R.drawable
//                .ic_category_t3), null));
//        mCategoryHomeBeenList.add(new CategoryHomeItemBean("舞 蹈", getDrawable(R.drawable
//                .ic_category_t129), null));
//        mCategoryHomeBeenList.add(new CategoryHomeItemBean("游 戏", getDrawable(R.drawable
//                .ic_category_t4), null));
//        mCategoryHomeBeenList.add(new CategoryHomeItemBean("科 技", getDrawable(R.drawable
//                .ic_category_t36), null));
//        mCategoryHomeBeenList.add(new CategoryHomeItemBean("生 活", getDrawable(R.drawable
//                .ic_category_t160), null));
//        mCategoryHomeBeenList.add(new CategoryHomeItemBean("鬼 畜", getDrawable(R.drawable
//                .ic_category_t119), null));
//        mCategoryHomeBeenList.add(new CategoryHomeItemBean("时 尚", getDrawable(R.drawable
//                .ic_category_t155), null));
//        mCategoryHomeBeenList.add(new CategoryHomeItemBean("娱 乐", getDrawable(R.drawable
//                .ic_category_t5), null));
//        mCategoryHomeBeenList.add(new CategoryHomeItemBean("电 影", getDrawable(R.drawable
//                .ic_category_t23), null));
//        mCategoryHomeBeenList.add(new CategoryHomeItemBean("电视剧", getDrawable(R.drawable
//                .ic_category_t11), null));
//        mCategoryHomeBeenList.add(new CategoryHomeItemBean("游戏中心", getDrawable(R.drawable
//                .ic_category_game_center), null));
    }

    private Drawable getDrawable(int id) {
        Drawable drawable = getResources().getDrawable(id);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        return drawable;
    }

}
